//
//  MHCircleHomeViewController.m
//  MiaoCircle
//
//  Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHDiscoverHomeViewController.h"
#import "MHCircleHomeViewModel.h"
#import "MHHomeCircleItemCollectionViewCell.h"
#import "MHCreateCircleCollectionViewCell.h"
#import "WZLBadgeImport.h"
#import "MHCollectionView.h"
#import "SDCycleScrollView.h"
#import "MHDiscoverHeaderReusableView.h"
#import "MHHomeDiscoverItemCollectionViewCell.h"
#import "MHDiscoverHomeViewModel.h"
#import "MHBanner.h"


@interface MHDiscoverHomeViewController ()
@property (nonatomic, readwrite, strong) MHDiscoverHomeViewModel *viewModel;

@end

@implementation MHDiscoverHomeViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView mh_registerNibCell:MHHomeDiscoverItemCollectionViewCell.class];
    [self.collectionView mh_registerNibCell:MHCreateCircleCollectionViewCell.class];
//    self.view.backgroundColor = MH_MAIN_BACKGROUNDCOLOR;
    self.collectionView.backgroundColor = MHColorFromHexString(@"#F8F8F8");
//    [self.collectionView registerClass:[MHDiscoverHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DiscoverHeaderView"];
    
    @weakify(self);
    [[MHNotificationCenter rac_addObserverForName:MHReloadHomeDiscoverDataNotification object:nil] subscribeNext:^(NSNotification *note) {
        @strongify(self);
        [self.viewModel.requestRemoteDataCommand execute:@1];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [SDImageCache sharedImageCache].config.shouldDecompressImages = NO ;
//    [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
    
    [self.navigationController.navigationBar setBackgroundImage:MHNavBackGround() forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.translucent = NO;
}

-(void)configurelayout:(CHTCollectionViewWaterfallLayout *)layout
{
//    layout.headerHeight = MHPxConvertToPt(254/2);
    layout.headerHeight = 0;
    layout.minimumColumnSpacing = 0;
    layout.minimumInteritemSpacing = 10;
    layout.columnCount = 2;

}



//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//
//    UICollectionReusableView *supplementaryView;
//
//    if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]){
//        MHDiscoverHeaderReusableView *view = (MHDiscoverHeaderReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:@"DiscoverHeaderView" forIndexPath:indexPath];
//        [[self.viewModel.fetchBannerListCommand execute:nil] subscribeNext:^(NSArray  *bannerList) {
//            NSMutableArray *array = [NSMutableArray array];
//            for (MHBanner *banner in bannerList) {
//                [array addObject:banner.allPath];
//            }
//            dispatch_async(dispatch_get_main_queue(), ^{
//                view.sdCycleScrollView.imageURLStringsGroup = array;
//            });
//
//        }];
////        view.sdCycleScrollView.imageURLStringsGroup = imagesURLStrings;
//        view.sdCycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
//        view.sdCycleScrollView.delegate = self;
////        view.sdCycleScrollView.titlesGroup = titles;
//        view.sdCycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
//        view.sdCycleScrollView.placeholderImage = MHDefaultAvatar(MHDefaultAvatarTypeDefualt);
//        supplementaryView = view;
//
//    }else
//    {
//        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
//
//        supplementaryView = footerview;
//    }
//    return supplementaryView;
//}


- (UICollectionViewCell *)collectionView:(MHCollectionView *)collectionView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
{
    MHHomeDiscoverItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MHHomeDiscoverItemCollectionViewCell.class) forIndexPath:indexPath];
    cell.positionBtn.hidden = !self.viewModel.showLoacation;
    return cell;
    
//    if (indexPath.item == self.viewModel.circles.count) {
//
//    }else
//    {
//        return [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MHHomeCircleItemCollectionViewCell.class) forIndexPath:indexPath];
//    }
    
}

- (UIEdgeInsets)contentInset{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)configureCell:(MHHomeDiscoverItemCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object
{
    [cell bindViewModel:object];
}

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    
    return YES;
    
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//   MHHomeDiscoverItemCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    MHHomeDiscoverItemViewModel *itemViewModel =  self.viewModel.dataSource[indexPath.item];
    return CGSizeMake(itemViewModel.cellSize.width, itemViewModel.cellSize.height);
}


@end
